<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Variables and Assignments: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby variables,ruby study notes,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<!-- Google +1 button code -->
<link rel="canonical" href="/satishtalim/variables_and_assignment.html" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->

</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Helping Ruby Programmers become Awesome!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="https://blog.rubylearning.github.io/" title="Ruby blog of Ruby Learning site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.github.io/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"> <a name="TemplateInfo"></a>
                <h1>Variables and Assignment</h1>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/fun_with_strings.html">&lt;Fun With Strings | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/scope.html">Scope&gt;</a>
                  </strong>
                </p>

                <p>To store a number or a string in your computer's memory for use later in your program, you need to give the number or string a name. Programmers often refer to this process as <i>assignment</i> and they call the names <i>variables</i>. A variable springs into existence as soon as the interpreter sees an assignment to that variable.</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                s = 'Hello World!'
                x = 10
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p class="post-note">
                A <i>bareword</i> is any combination of letters, numbers, and underscores, and is not qualified by any symbols (<b>Reference</b>: <a href="https://web.archive.org/web/20181219143329/http://alumnus.caltech.edu/~svhwan/prodScript/avoidBarewords.html">http://alumnus.caltech.edu/~svhwan/prodScript/avoidBarewords.html</a>). Local variables and <i>barewords</i> look similar; they must start with either the underscore character (_) or a lowercase letter, and they must consist entirely of letters, numbers, and underscores. Remember, local variable references look just like method invocation expressions and <i>Keywords</i> can't be used as variable names.<br /><br /><i>Method calls</i> can also be barewords, such as my_method. <strong>gets</strong> is a method call; so is <strong>system</strong>. Whenever Ruby sees a bareword, it interprets it as one of three things: (a) If there's an equal sign (=) to the right of the bareword, it's a <i>local variable</i> undergoing an assignment. (b) Ruby has an internal list of keywords and a bareword could be a <i>keyword</i>.(c) If the bareword is not (a) or (b) above, the bareword is assumed to be a <i>method call</i>. If no method by that name exists, Ruby raises a <strong>NameError</strong>.
                </p>

                <p>The <strong>p004stringusage.rb</strong> shows us some more usage with strings.
                </p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # p004stringusage.rb
                # Defining a constant
                PI = 3.1416
                puts PI
                # Defining a local variable
                my_string = 'I love my city, Pune'
                puts my_string
                =begin
                  Conversions
                  .to_i, .to_f, .to_s
                =end
                var1 = 5;
                var2 = '2'
                puts var1 + var2.to_i
                # &lt;&lt; appending to a string
                a = 'hello '
                a&lt;&lt;'world.
                I love this world...'
                puts a
                =begin
                  &lt;&lt; marks the start of the string literal and
                  is followed by a delimiter of your choice.
                  The string literal then starts from the next
                  new line and finishes when the delimiter is
                  repeated again on a line on its own. This is known as
                  Here document syntax.
                =end
                a = &lt;&lt;END_STR
                This is the string
                And a second line
                END_STR
                puts a
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p class="post-note">
                In the example:<br />
                x = "200.0".to_f<br />
                the dot means that the message "to_f" is being sent to the string "200.0", or that the method to_f is being called on the string "200.0". The string "200.0" is called the receiver of the message.  Thus, when you see a dot in this context, you should interpret it as a message (on the right) being sent to an object (on the left).
                </p>

                <h3>Summary</h3>

                <p>I have listed down all the <strong><a href="/satishtalim/ruby_summary1.html">important points</a></strong> you need to remember after you have completed the following topics: <a href="/satishtalim/introduction.html">Introduction</a>, <a href="/satishtalim/ruby_installation.html">Installation</a>, <a href="/satishtalim/first_ruby_program.html">First Ruby program</a>, <a href="/satishtalim/features.html">Features of Ruby</a>, <a href="/satishtalim/numbers_in_ruby.html">Numbers in Ruby</a>, <a href="/satishtalim/fun_with_strings.html">String fundamentals</a> and <a href="/satishtalim/variables_and_assignment.html">Variables and Assignment</a>.</p>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them. Much of the material on <a href="/">rubylearning.github.io</a> and in the course at <a href="http://rubylearning.org/">rubylearning.org</a> is drawn <strong>primarily</strong> from the <strong>Programming Ruby book</strong>, available from <a href="http://pragprog.com/book/ruby3/programming-ruby-1-9-2-0">The Pragmatic Bookshelf</a>.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/fun_with_strings.html">&lt;Fun With Strings | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/scope.html">Scope&gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
</div>

<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
